package com.dawnling.countdownlistdemo.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具类
 * github:https://github.com/KnifeStone/Hyena
 * Created by KnifeStone on 2017-6-21.
 */
public class TimeUtils {

    /**
     * 时间戳转友好型显示
     * 规则0：01:21
     * 单位：秒
     */
    public static String long2String0(long time) {
        time = Math.abs(time);
        int hour = (int) (time / (60 * 60));
        time = time % (60 * 60);
        int minute = (int) (time / 60);
        String str = "";
        if (hour == 0) {
        } else if (hour >= 10) {
            str = hour + ":";
        } else {
            str = "0" + hour + ":";
        }
        if (minute >= 10) {
            str = str + minute;
        } else {
            str = str + "0" + minute;
        }
        return str;
    }

    /**
     * 时间戳转友好型显示
     * 规则1：01时21分10秒
     * 单位：秒
     */
    public static String long2String1(long time) {
        time = Math.abs(time);
        int hour = (int) (time / (60 * 60));
        time = time % (60 * 60);
        int minute = (int) (time / 60);
        int second = (int) (time % (60));
        String str = "";
        if (hour == 0) {
        } else if (hour >= 10) {
            str = hour + "时";
        } else {
            str = "0" + hour + "时";
        }
        if (minute >= 10) {
            str = str + minute + "分";
        } else {
            str = str + "0" + minute + "分";
        }
        if (second >= 10) {
            str = str + second + "秒";
        } else {
            str = str + "0" + second + "秒";
        }
        return str;
    }

    /**
     * 时间戳转友好型显示
     * 规则2   01：21：10
     * 单位：秒
     */
    public static String long2String3(long time) {
        time = Math.abs(time);
        int hour = (int) (time / (60 * 60));
        time = time % (60 * 60);
        int minute = (int) (time / 60);
        int second = (int) (time % (60));
        String str = "";
        if (hour == 0) {
        } else if (hour >= 10) {
            str = hour + ":";
        } else {
            str = "0" + hour + ":";
        }
        if (minute >= 10) {
            str = str + minute + ":";
        } else {
            str = str + "0" + minute + ":";
        }
        if (second >= 10) {
            str = str + second;
        } else {
            str = str + "0" + second;
        }
        return str;
    }

    /**
     * 时间戳转友好型显示
     * 规则2：不显示年和秒
     */
    public static String long2String2(String str) {
        if (str == null) {
            return "系统时间";
        }
        if (str.length() == 19) {
            return str.substring(5, 16);
        }
        return str;
    }

    public static String getCurrenTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String currenTime = format.format(curDate);
        return currenTime;
    }
}
